package creational_pattern.factory_method_pattern;

public class FactoryMethodPattern {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //可通过DI
        //LoggerFactory loggerFactory = new FileLoggerFactory();
        Class<?> cl = Class.forName("creational_pattern.factory_method_pattern.FileLoggerFactory");
        LoggerFactory loggerFactory = (LoggerFactory) cl.newInstance();
        Logger logger = loggerFactory.createLogger();
        logger.writeLog();
    }
}
//产品接口
interface Logger{
    void writeLog();
}
class FileLogger implements Logger{
    @Override
    public void writeLog() {
        System.out.println("file logging");
    }
}
class DatabaseLogger implements Logger{

    @Override
    public void writeLog() {
        System.out.println("database logging");
    }
}

interface LoggerFactory{
    public abstract Logger createLogger();
}

class FileLoggerFactory implements LoggerFactory{

    @Override
    public Logger createLogger() {
        //对象初始化前的准备工作
        return new FileLogger();
    }
}
class DatabaseLoggerFactory implements LoggerFactory{
    @Override
    public Logger createLogger() {
        //对象初始化前的准备工作
        return new DatabaseLogger();
    }
}
